home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 976 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.5 KB

  1. Path: news.sprintlink.net!datalytics!news
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: about for loop
  5. Date: 8 Jan 1996 19:16:37 GMT
  6. Organization: Datalytics, Inc
  7. Message-ID: <4crqil$h4r@gold.datalytics.com>
  8. References: <4civik$cmg@peter.pu.edu.tw> <4cjdlq$hc5@xpat.postech.ac.kr>
  9. NNTP-Posting-Host: pc071.datalytics.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. madincom@reuse.postech.ac.kr (Oh Jun-hee) wrote:
  16. >s8311116@csim49.pu.edu.tw wrote:
  17. >>   if i write this for loop"for(i=0 ; i<=10 ; i++ )
  18. >>                            cout<< i ;
  19. >>   the result should be " 12345678910 " or " 0123456789 " ?
  20. >>   and what is "double linked list " ??
  21. >>   thanks!
  22. >
  23. >I. the result of "for loop" is 0123456789 ...
  24. >
  25. >for (initialization code;
  26. >    condition for continue;
  27. >    the instruction which will be executed    after each loop)
  28. >                                            >So, the instruction i++ will be first executed    after first loop.
  29. >
  30.  
  31. We can make this even easier to understand.  A for loop is 
  32. exactly equivalent to a while loop in this way:
  33.  
  34. for (a; b; c)
  35. {
  36.     d;
  37. }
  38.  
  39. becomes:
  40.  
  41. a;
  42. while (b)
  43. {
  44.     d;
  45.     c;
  46. }
  47.  
  48. So, your original loop becomes this:
  49.  
  50. i = 0;
  51. while (i <= 10)
  52. {
  53.     cout << i;
  54.     i++;
  55. }
  56.  
  57. As you can see, this will insert 0, 1, 2,..., 9, 10 onto cout, 
  58. giving the result "012345678910."
  59.  
  60. -- 
  61. Robert Stewart        | My opinions are usually my own.
  62. Datalytics, Inc.
  63. (513)226-7700
  64. stew@datalytics.com
  65.  
  66.  
  67.